
capture drop xx
capture drop trades_with_relational
gen xx=D_sourcing
replace xx=0 if D_sourcing==.
bys s_id: egen trades_with_relational=max(xx)

capture drop sample
capture drop s_in_sample
gen sample=(S_pds==1 & n_ud==1) 
bys s_id: egen s_in_sample=max(sample)


capture drop _merge
sort UD_num
merge m:1 UD_num using  "${source_data}/Destinations_Orders.dta" 

cap drop gr_sbt
egen gr_sbt=group(gr_st b_id)

cap drop ln_q_st_all
cap drop num_b_st
bys gr_st: egen num_o_st=nvals(UD_num)
bys gr_st: egen ln_q_st_all=total(q_exp)
bys gr_sbt: egen sh_sbt_samp=total(q_exp)
replace sh_sbt_samp=sh_sbt_samp/ln_q_st_all
replace ln_q_st_all=ln(ln_q_st_all)

bys gr_st: egen ln_med_sh_b_st=median(sh_sbt_samp)
bys gr_st: egen ln_max_sh_b_st=max(sh_sbt_samp)
replace ln_med_sh_b_st=ln(ln_med_sh_b_st)
replace ln_max_sh_b_st=ln(ln_max_sh_b_st)
bys gr_st: egen num_b_st=nvals(b_id)
bys gr_st: egen num_j_st=nvals(HS6_main)
bys gr_st: egen num_d_st=nvals(dest_order)

replace num_b_st=ln(num_b_st)
replace num_o_st=ln(num_o_st)
replace num_d_st=ln(num_d_st)
replace num_j_st=ln(num_j_st)

qui reghdfe  num_o_st  trades_with_relational    ///
if n_st==1 & s_in_sample==1, abs(year) vce (cluster s_id)
capture drop x_sample
gen x_sample=e(sample)

label var ln_q_st_all "\$q_{sy}\$"
label var trades_with_relational "\$Trades \ w/ Relational_{s}\$"
label var num_j_st "\$Count^j_{sy}\$"
label var num_d_st "\$Count^d_{sy}\$"
label var num_b_st "\$Count^b_{sy}\$"
label var num_o_st "\$Count^o_{sy}\$"
label var ln_med_sh_b_st "\$Med \ Share^b_{sy}\$"
label var ln_max_sh_b_st "\$Max \ Share^b_{sy}\$"

* Seller Characteristics (Not conditional on size)

eststo clear
eststo: reghdfe  ln_q_st_all trades_with_relational    ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace    

eststo: reghdfe  num_j_st trades_with_relational    ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace


eststo: reghdfe  num_d_st trades_with_relational    ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace
	
eststo: reghdfe  num_b_st trades_with_relational    ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace
   
eststo: reghdfe  num_o_st trades_with_relational    ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace
			
 esttab using "${tables}\TableC11PanelA.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
  drop(_cons ) order( ) /// 
  stats(CTRL2 r2   N   , fmt( %~#s 2 %12.0fc  )  ///
  labels("FEs" `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes  	

* Seller Characteristics (Conditional on size)

eststo clear    
eststo: reghdfe  num_j_st trades_with_relational  ln_q_st_all   ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace	  
    
eststo: reghdfe  num_d_st trades_with_relational  ln_q_st_all   ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace	  
  
eststo: reghdfe  num_b_st trades_with_relational  ln_q_st_all   ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace	  
 
eststo: reghdfe  num_o_st trades_with_relational  ln_q_st_all   ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace		 
 
eststo: reghdfe  ln_med_sh_b_st  trades_with_relational  ln_q_st_all  ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace     
	
eststo: reghdfe  ln_max_sh_b_st  trades_with_relational  ln_q_st_all   ///
if  n_st==1 & x_sample==1, abs(year) vce (cluster s_id)
	quietly estadd local CTRL2 ="y", replace    	

 esttab using "${tables}\TableC11PanelB.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
  drop(ln_q_st_all _cons) order( ) /// 
  stats(CTRL2 r2   N   , fmt( %~#s 2 %12.0fc  )  ///
  labels("FEs" `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes  	
  

* Order Characteristics (Conditional on size)
  
capture drop sample
gen sample=(S_pds==1 & n_ud==1) 

eststo clear
eststo: reghdfe ln_q_sbjo   trades_with_relational  ln_q_st_all  ///
if sample==1, ///
abs(gr_bjt ) vce (cluster gr_sjt)
  quietly estadd local CTRL ="No", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="bjy", replace  
  quietly estadd local CTRL2 ="Yes", replace    
  quietly estadd scalar  U =  . , replace     

eststo: reghdfe ln_av_q_ship_sbjo   trades_with_relational  ln_q_st_all  ///
if sample==1, ///
abs(gr_bjt ) vce (cluster gr_sjt)
  quietly estadd local CTRL ="No", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="bjy", replace  
  quietly estadd local CTRL2 ="Yes", replace    
  quietly estadd scalar  U =  . , replace  

eststo: reghdfe ln_num_l_o   trades_with_relational  ln_q_st_all  ///
if sample==1, ///
abs(gr_bjt ) vce (cluster gr_sjt)
  quietly estadd local CTRL ="No", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="bjy", replace  
  quietly estadd local CTRL2 ="Yes", replace    
  quietly estadd scalar  U =  . , replace  
  
eststo: reghdfe ln_num_l_o   trades_with_relational  ln_q_st_all  ln_q_sbjo  ///
if sample==1, ///
abs(gr_bjt ) vce (cluster gr_sjt)
  quietly estadd local CTRL ="No", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="bjy", replace  
  quietly estadd local CTRL2 ="Yes", replace    
  quietly estadd scalar  U =  . , replace     

esttab using "${tables}\TableC11PanelC.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
  drop(ln_q_st_all  ln_q_sbjo _cons) order( ) /// 
  stats(FE r2   N   , fmt( %~#s 2 %12.0fc  )  ///
  labels("FEs" `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes   
 
